package com.dang.alex.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.dang.alex.dao.ProductDAO;
import com.dang.alex.model.Book;
import com.dang.alex.model.Product;
import com.dang.alex.util.DBConnection;

public class ProductDAOImp implements ProductDAO {
	private final static String FIND_BY_ID="select * from d_product p left join d_book b on b.id=p.id where p.id=?";
	private final static String FIND_BY_NEW="select p.*,b.* from d_product p JOIN d_book b on p.id=b.id ORDER BY p.add_time desc limit 0,?";
	private final static String FIND_BY_HOT="select sum(product_num) as num,p.*,b.* from d_item i join d_product p on i.product_id=p.id join d_book b on i.product_id=b.id group by product_id order by num desc limit 0,?";
	private final static String FIND_ALL="select * from d_product";
	public Book findById(int id) throws Exception {
		Connection conn=null;
		PreparedStatement prep=null;
		ResultSet rs=null;
		Book book=null;
		try {
			prep=DBConnection.getConnection().prepareStatement(FIND_BY_ID);
			prep.setInt(1, id);
			rs=prep.executeQuery();
			if(rs.next()) {
				book=new Book();
				book.setId(rs.getInt("id"));
				book.setProductName(rs.getString("product_name"));
				book.setFixedPrice(rs.getDouble("fixed_price"));
				book.setDangPrice(rs.getDouble("dang_price"));
				book.setProductPic(rs.getString("product_pic"));
				book.setAddTime(rs.getLong("add_time"));
				book.setAuthor(rs.getString("author"));
				book.setPublishing(rs.getString("publishing"));
				book.setPublishDT(rs.getLong("publish_time"));
				book.setAuthorSummary(rs.getString("author_summary"));
				book.setCatalogue(rs.getString("catalogue"));
				book.setDescription(rs.getString("description"));
				book.setHasDeleted(rs.getInt("has_deleted"));
				book.setIsbn(rs.getString("isbn"));
				book.setKeywords(rs.getString("keywords"));
				book.setPrintNumber(rs.getString("print_number"));
				book.setPrintTime(rs.getLong("print_time"));
				book.setTotalPage(rs.getString("total_page"));
				book.setWhichEdtion(rs.getString("which_edtion"));
				book.setWordNumber(rs.getString("word_number"));
			}
		}finally {
			DBConnection.close(rs, prep, conn);
		}
		return book;
	}
	public List<Product> findByHot(int id)throws Exception{
		Connection conn=null;
		PreparedStatement prep=null;
		ResultSet rs=null;
		List<Product>pros=new ArrayList<Product>();
		try {
			prep=DBConnection.getConnection().prepareStatement(FIND_BY_HOT);
			prep.setInt(1, id);
			rs=prep.executeQuery();
			while(rs.next()) {
				Book book=new Book();
				book.setProductName(rs.getString("product_name"));
				book.setProductPic(rs.getString("product_pic"));
				book.setFixedPrice(rs.getDouble("fixed_price"));
				book.setDangPrice(rs.getDouble("dang_price"));
				
				book.setPrintTime(rs.getLong("print_time"));
				book.setDescription(rs.getString("description"));
				book.setAddTime(rs.getLong("add_time"));
				book.setKeywords(rs.getString("keywords"));
				book.setHasDeleted(rs.getInt("has_deleted"));
				pros.add(book);
			}
		}finally {
			DBConnection.close(rs, prep, conn);
		}
		return pros;
	}
	@Override
	public List<Book> findByNew(int id) throws Exception {
		PreparedStatement prep=null;
		Connection conn=null;
		ResultSet rs=null;
		List<Book>books=new ArrayList<Book>();
		try {
			prep=DBConnection.getConnection().prepareStatement(FIND_BY_NEW);
			prep.setInt(1, id);
			rs=prep.executeQuery();
			while(rs.next()) {
				Book book=new Book();
				book.setProductName(rs.getString("product_name"));
				book.setProductPic(rs.getString("product_pic"));
				book.setFixedPrice(rs.getDouble("fixed_price"));
				book.setDangPrice(rs.getDouble("dang_price"));
				
				book.setPrintTime(rs.getLong("print_time"));
				book.setDescription(rs.getString("description"));
				book.setAddTime(rs.getLong("add_time"));
				book.setKeywords(rs.getString("keywords"));
				book.setHasDeleted(rs.getInt("has_deleted"));
				books.add(book);
			}
		}finally {
			DBConnection.close(rs, prep, conn);
		}
		return books;
	}
	@Override
	public List<Book> findAll() throws Exception {
		PreparedStatement prep=null;
		Connection conn=null;
		ResultSet rs=null;
		List<Book>books=new ArrayList<Book>();
		try {
			prep=DBConnection.getConnection().prepareStatement(FIND_ALL);
			rs=prep.executeQuery();
			while(rs.next()) {
				Book book=new Book();
				book.setId(rs.getInt("id"));
				book.setProductName(rs.getString("product_name"));
				book.setDescription(rs.getString("description"));
				book.setAddTime(rs.getLong("add_time"));
				book.setFixedPrice(rs.getDouble("fixed_price"));
				book.setDangPrice(rs.getDouble("dang_price"));
				book.setKeywords(rs.getString("keywords"));
				book.setHasDeleted(rs.getInt("has_deleted"));
				book.setProductPic(rs.getString("product_pic"));
				book.setPrintTime(rs.getLong("print_time"));
				books.add(book);
			}
		}finally {
			DBConnection.close(rs, prep, conn);
		}
		return books;
	}
}
